package com.jet.chat.client;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

import com.jet.chat.exception.ChatException;
import com.jet.chat.exception.ChatExceptionClientReaderNotAvailable;
import com.jet.chat.exception.ChatExceptionClientWriterNotAvailable;
import com.jet.chat.exception.ChatExceptionServerHostNotAvailable;
import com.jet.chat.message.MessageReaderBufferedReader;
import com.jet.chat.message.MessageWriterObjectOutputStream;
import com.jet.chat.service.Arguments;
import com.jet.chat.thread.ChatThread;

public class ChatClientTwistedWriter {
	private Socket clientSocket;
	private ObjectOutputStream clientWriter;
	
	public ChatClientTwistedWriter(String clientHost, int clientPort) throws ChatExceptionServerHostNotAvailable, ChatExceptionClientReaderNotAvailable, ChatExceptionClientWriterNotAvailable {
		try {
			this.clientSocket = new Socket(clientHost, clientPort);
		} catch (UnknownHostException e) {
			throw new ChatExceptionServerHostNotAvailable("host not found");
		} catch (IOException e) {
			throw new ChatExceptionServerHostNotAvailable("ololo netnet net");
		}

		try {
			this.clientWriter = new ObjectOutputStream(clientSocket.getOutputStream());
		} catch (IOException e) {
			throw new ChatExceptionClientWriterNotAvailable("ошибка создания потока вывода клиента");
		}
		
	}

	public void execute() throws ChatException {
		new Thread(new ChatThread(new MessageReaderBufferedReader(new BufferedReader(new InputStreamReader(System.in))), new MessageWriterObjectOutputStream(clientWriter), "client shutdown")).start();
		System.out.println("ChatClientTwistedWriter started");
	}

	public static void main(String[] arg) {
		ChatClientTwistedWriter chatClientTwistedWriter;
		try {
			String clientServerHost = "localhost";
			int clientServerPort = 30300;

			Arguments arguments = new Arguments(arg);

			String clientServerPortArg = arguments.getValue("clientServerPort");
			if (clientServerPortArg != null && !clientServerPortArg.equals("")) {
				clientServerPort = Integer.parseInt(clientServerPortArg);
			}
			
			System.out.println("clientServerPort: " + clientServerPort);
			
			chatClientTwistedWriter = new ChatClientTwistedWriter(clientServerHost, clientServerPort);
			chatClientTwistedWriter.execute();
		} catch (ChatExceptionServerHostNotAvailable e) {
			System.out.println("Клиентский сервер недоступен - сначала запустите: " + ChatClientTwistedReader.class.toString());
			 //[ek] TODO need more user-friendly messages
		} catch (ChatExceptionClientReaderNotAvailable e) {
			System.out.println("Ошибка потока чтения клиента");
		} catch (ChatExceptionClientWriterNotAvailable e) {
			System.out.println("Ошибка потока вывода клиента");
		} catch (ChatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
